/**
 * Created by shidaiapp on 2017/9/29.
 */
import actionUrls from "../../../../api/ApiUrls";
import {createDownloadForm} from "../../../js/downloadUtils"
import Vue from "vue";

export const init = (tiyData) => {
  tiyData.tData.userAnalysisList.splice(0, tiyData.tData.userAnalysisList.length);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 60;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.searchParams.startDate = "";
  tiyData.tParams.searchParams.endDate = "";
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchService = (self, tiyData) => {

  let startDate,endDate;
  self.$Spin.show();
  if(tiyData.tParams.searchParams.startDate){
    startDate = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.searchParams.startDate).getTime())

  }
  if(tiyData.tParams.searchParams.endDate){
    endDate =Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.searchParams.endDate).getTime())
  }
  self.$apiAnalysis.userAnalysisList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "startDate" : startDate,
    "endDate" : endDate,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let userPage = response.result;
      tiyData.tPage.total = userPage.total;
      tiyData.tData.userAnalysisList.splice(0, tiyData.tData.userAnalysisList.length);
      userPage.list.forEach(
        function (item) {
          tiyData.tData.userAnalysisList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const exportAnalysis = (self, tiyData) => {
  self.$Spin.show();
  let startDate,endDate;
  if(tiyData.tParams.searchParams.startDate){
    startDate = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.searchParams.startDate).getTime())
  }
  if(tiyData.tParams.searchParams.endDate){
    endDate = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.searchParams.endDate).getTime())
  }
  self.$apiAnalysis.exportUserAnalysis({
    "startDate" : startDate,
    "endDate" : endDate,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let downloadForm = createDownloadForm(response.result, actionUrls.URL_DOWNLOAD_EXCEL);
      downloadForm.submit();
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
